<?php
import('library.sm');
include_class("clientmodules");

class ConfigModule extends Module
{
	public $admin = NULL;
	
	
	public function __config()
	{
		$this->page->template = "control/index.tpl";
		$this->locale->add("control.ini");
		if ( !$this->site->admin_is_auth() )
		{
			$this->site->admin_clear_auth();
			header('Location: /panel/login');
		}
		
		$this->helper = new SMHelper( $this->site );
		$this->admin = new AdminAccess( intval($_SESSION['admin_id']) );
		
		$def = $this->helper->defaults();
		$this->helper->setMenuActive("config");
		
	}
	
	
	public function __default()
	{
		global $_baseConfig;
		
		$this->page->title("_configPageBase", true);
		$this->helper->setSubMenuActive('base');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/edit_form.tpl");
		$this->locale->addFullPath(_RLOCAL."international.ini");
		$this->helper->addBTB("/panel/config/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
		$store_domain = $this->site->_domain;
		$store = ORM::for_table('store')->where('domain', $this->site->_domain)->find_one();
		
		$this->page->setVar('config_infos');
		
		if ( isset( $_SESSION['module_config_base'] ) && $_SESSION['module_config_base'] == 'saved' )
		{
			$error = new Template("stores/small_success.tpl");
			$error->setLocVar("successText", "_text_configSaved");
			$this->page->append("config_infos", $error->processing());
			unset($_SESSION['module_config_base']);
		}
		
		if ( $store )
		{
			$this->page->setVar('stores_no_errors', true);
			$this->page->setVar("setDomain", $store_domain);
			$this->page->setVar("BaseSiteDomain", $_baseConfig->get("Site", "domain"));
			$this->page->setVar("stores_errors");
			$this->page->setVar("setTitle", $store['title']);
			$this->page->setVar('setEmail', $store['email']);
			$this->page->setVar('setPhone', $store['phone']);
			$this->page->setVar('setFax', $store['fax']);
			
			// Languages list
			$lang_list = array();
			$langs = explode(';', $store['languages']);
			if ( $dir = opendir(_RLOCAL) )
			{
				
				$curlang = $this->locale->getLanguage();
				while( ( $file = readdir( $dir ) ) !== false )
				{
					if ( $file == ".." || $file == "." || !is_dir( _RLOCAL . $file ) ) continue;
					
					$lang_list[$file] = array(
												$this->locale->get( "_".$file, $curlang ),
												array_search( strval($file), $langs ) === false ? false : true
											);
				}
			}
			$this->page->setVar("lang_list", $lang_list);
			
			// Themes list
			$theme_list = array();
			if ( $dir = opendir(_RTMPL) )
			{
				while( ( $file = readdir( $dir ) ) !== false )
				{
					if ( $file == ".." || $file == "." ) continue;
					if ( is_dir( _RTMPL . $file ) ) $theme_list[] = array($file, $store['template'] == $file ? "yes" : "no");
				}
			}
			$this->page->setVar("theme_list", $theme_list);
			
			
			$this->page->setVar("activate_after_def", intval($store['locked']) == 0 ? false : true);
		}
		else
		{
			$this->page->setVar('stores_no_errors', false);
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesNotExists");
			$this->page->append("config_infos", $error->processing());
		}
		
	}
	public function BasesaveAction()
	{
		$this->page->title("_configPageBase", true);
		$this->helper->setSubMenuActive('base');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/edit_form.tpl");
		$this->locale->addFullPath(_RLOCAL."international.ini");
		$this->helper->addBTB("/panel/config/", "_menuHomePage", BTNCOLOR_BASE);
		$this->helper->addBTB('/panel/config/', '_Back', BTNCOLOR_RED);
		
		// -----------------------------------------------------------------------------------------------------
		$store_create_vars = $this->site->post;
		
		$domain = $this->site->_post('domain');
		$title = $this->site->_post('title');
		$theme = $this->site->_post('theme') ?: "default";
		$activate = $this->site->_post('activate') ?: "off";
		$email = $this->site->_post('email');
		$phone = $this->site->_post('phone');
		$fax = $this->site->_post('fax') ?: $this->locale->get("_NoFax");
		$langs = array();
		
		unset(
			$store_create_vars['domain'],
			$store_create_vars['title'],
			$store_create_vars['theme'],
			$store_create_vars['activate'],
			$store_create_vars['email'],
			$store_create_vars['phone'],
			$store_create_vars['fax']
		);
		
		if ( count($store_create_vars) > 0 )
		{
			foreach($store_create_vars as $key=>$val)
			{
				if ( $val == "on" )
				{
					$langs[] = str_replace("lang_", "", $key);
					unset($store_create_vars[$key]);
				} 
			}
		}
		
		// -----------------------------------------------------------------------------------------------------
		
		$this->page->loadTplToVar("content", "config/create_affirm.tpl");
		
		$error = false;
		
		if ( strlen($domain) < 1 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesDomainLenSmall");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		$check_domain = ORM::for_table('store')->where('domain', $domain)->find_one();
		if ( !$check_domain )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesNotExists");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		if ( strlen( $title ) < 3 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesSmallTitleLen");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		if ( count($langs) < 1 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesNoLangs");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		if ( !$phone || $phone == "" )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesPhoneNotExists");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		if ( !$email || $email == "" )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_storesEmailSupportNotExists");
			$this->page->append("stores_errors", $error->processing());
			$error = true;
		}
		
		if ( $error )
		{
			$this->page->setVar("content", false);
		}
		else
		{
			$this->page->setVar("stores_errors", "");
			$this->page->setVar("content", false);
			$languages = array();
			foreach($langs as $key=>$val ) $languages[$key] = $val;
			
			$data = ORM::for_table('store')->where('domain', $domain)->find_one();
			$data->title = $title;
			//qr($activate);
			$data->locked = $activate == "on" ? 1 : 0;
			$data->template = $theme;
			$data->languages = implode(";",$languages);
			$data->phone = $phone;
			$data->fax = $fax;
			$data->email = $email;
			$data->save();
			
			$_SESSION['module_config_base'] = 'saved'; 
			
			header("Location: /panel/config/");
		}
	}
	
	
	public function ImagesAction()
	{
		$this->page->title("_configPageImages", true);
		$this->helper->setSubMenuActive('images');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
	}
	public function ImagessaveAction()
	{}
	
	public function LocaleAction()
	{
		$this->page->title("_configPageLocale", true);
		$this->helper->setSubMenuActive('locale');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
	}
	public function LocalesaveAction()
	{}
	
	
	public function StoreAction()
	{
		$this->page->title("_configPageStore", true);
		$this->helper->setSubMenuActive('store');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		// -----------------------------------------------------------------------------------------------------
		
		
		
		
	}
	public function StoresaveAction()
	{
		
	}
	
	public function PayAction()
	{
		$this->page->title("_configPagePay", true);
		$this->helper->setSubMenuActive('pay');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
	}
	
	public function NotifsAction()
	{
		$this->page->title("_configPageNotifs", true);
		$this->helper->setSubMenuActive('notifs');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
	}
	
	public function StocksAction()
	{
		$this->page->title("_configPageStocks", true);
		$this->helper->setSubMenuActive('stocks');
		$this->page->loadTplToVar("body", "config/base.tpl");
		$this->page->loadTplToVar("content", "config/page_base.tpl");
		
		$this->helper->addBTB("/panel/control/", "_menuHomePage", BTNCOLOR_BASE);
		
		// -----------------------------------------------------------------------------------------------------
	}
	
}